package com.boarsoft.boar.soagov.nexus.service;

import com.boarsoft.boar.entity.AppInfo;
import com.boarsoft.boar.entity.SoaNexus;
import com.boarsoft.boar.soagov.entity.FaceInfo;
import com.boarsoft.boar.soagov.entity.SvcInfo;
import com.boarsoft.boar.soagov.face.FaceBiz;
import com.boarsoft.boar.soagov.nexus.NexusBiz;
import com.boarsoft.boar.soagov.svc.NexusService;
import com.boarsoft.boar.soagov.svc.SvcBiz;
//import com.boarsoft.common.util.HttpClientUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

import com.boarsoft.boar.app.AppBiz;

/**
 *
 */
@Component("nexusService")
public class NexusServiceImpl implements NexusService {

    @Autowired
    private SvcBiz svcBiz;

    @Autowired
    private NexusBiz nexusBiz;

    @Autowired
    private AppBiz appBiz;

    //interface
    @Autowired
    private FaceBiz faceBiz;
    /**
     * 默认的应用版本号
     */
    private static final String DEFAULT_APP_VERSION = "1.0";

    /**
     * 3、
     * step1: 应用启动时调soagov的接口，传入其所依赖的接口列表，
     * step2: soagov记录服务依赖关系到数据库(soa_nexus)，再返回这些接口的配置，(svc_info)
     * <p>
     * 4、
     * 调用配置中心API，将应用与这些接口配置变量绑定(app_var表)
     *
     * @param appCode 应用名称
     * @param consume 依赖服务列表
     */
    @Override
    public List<SvcInfo> save(String appCode, List<SvcInfo> consume) {
        //0、根据appCode和版本号查询 AppInfo 表
        //AppInfo appInfo = appBiz.find(appCode, ver);
        AppInfo appInfo = appBiz.find(appCode);
        if (appInfo == null) {
            return null;
        }
        List<SvcInfo> detailInfos = new ArrayList<>();
        //遍历consume 插入或更新soa_nexus，同时查询svc_info 返回svc_info列表
        for (SvcInfo svcInfo : consume) {
            FaceInfo faceInfo = faceBiz.find(svcInfo.getFaceId());
            //1、查询 svc_info  根据code查询 find(String group, String name, String face, String version);
            SvcInfo detailInfo = svcBiz.find(svcInfo.getCatalog(), svcInfo.getName(), svcInfo.getFaceId(), faceInfo.getVer());
            detailInfos.add(detailInfo);

            //2、保存或更新soa_nexus表
            SoaNexus nexus = initNexus(appInfo, appCode, detailInfo);
            nexusBiz.save(nexus);
        }
        //调用配置中心API，将应用与这些接口配置变量绑定(app_var表)
        //TODO
        //TODO
        //com.boarsoft.common.util.HttpClientUtil;
        return detailInfos;
    }

    private SoaNexus initNexus(AppInfo appInfo, String appCode, SvcInfo svcInfo) {
        //list(String objType0, String objId0, String group0,
        //     String objType1, String objId1, String group1);
        List<SoaNexus> list = nexusBiz.list(String.valueOf(SvcInfo.TYPE_SRVC), appInfo.getId(), "default",
                String.valueOf(SvcInfo.TYPE_SRVC), svcInfo.getId(), "default");
        SoaNexus nexus = null;
        if (list.isEmpty()) {
            nexus = new SoaNexus();
        } else {
            nexus = list.get(0);
        }
        //private String id;
        /**
         * 引用方
         */
        //引用方ID
        nexus.setObjId0(appInfo.getId());
        //引用方类型(无需流程的普通服务)
        nexus.setObjType0(String.valueOf(SvcInfo.TYPE_SRVC));
        //private String objGroup0 = "default";
        //private String objCode0;
        //private String objName0;
        //private String objVer0;

        /**
         * 被引用方
         */
        //private String objId1;
        nexus.setObjId1(svcInfo.getId());
        //private String objType1;
        nexus.setObjType1(String.valueOf(SvcInfo.TYPE_SRVC));
        //private String objGroup1 = "default";
        //private String objCode1;
        //private String objName1;
        nexus.setObjName1(svcInfo.getName());
        //private String objVer1;

        //private String way;
        //private short async = 0;
        //private String memo = "";
        return nexus;
    }

}
